﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using University.Workshop.Business;

namespace University.Workshop.WinApp.Users
{
    public partial class FormUserView : Form
    {
        public static string value;
        public FormUserView()
        {
            InitializeComponent();
        }

        private void FormUserView_Load(object sender, EventArgs e)
        {
            radioButtonAdministrative.Select();
        }

        #region Main user
        private void buttonRegisterUser_Click(object sender, EventArgs e)
        {
            tabControlUserManager.SelectedTab = tabPageRegisterUser;
        }

        private void buttonSearchUser_Click(object sender, EventArgs e)
        {
            tabControlUserManager.SelectedTab = tabPageSearchUser;
        }

        private void buttonCloseUserManager_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region Register user
        private void buttonRegister_Click(object sender, EventArgs e)
        {
            string ocupation = OcupationSelected();
            string message = UserController.InsertUser(textBoxId.Text, textBoxName.Text, textBoxSurname1.Text, textBoxSurname2.Text, textBoxTelephone.Text, textBoxCellPhone.Text, textBoxAddress.Text, textBoxPassword.Text, textBoxPasswordConfirm.Text, ocupation);
            if (message == "Usuario almacenado!")
            {
                MessageBox.Show(this, message, "Registro exitoso", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);
                tabControlUserManager.SelectedTab = tabPageMainUser;
                CleanAllRegisterUserFills();
            }
            else
                MessageBox.Show(null, message,"Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            tabControlUserManager.SelectedTab = tabPageMainUser;
            CleanAllRegisterUserFills();
        }

        private string OcupationSelected()
        {
            if (radioButtonAdministrative.Checked)
                return "Administracion";
            else
                if (radioButtonSell.Checked)
                    return "Cajero";
                else
                    if (radioButtonBuy.Checked)
                        return "Almacen";
                    else
                        return "Ninguno";
        }

        private void CleanAllRegisterUserFills()
        {
            textBoxId.Clear(); textBoxName.Clear();
            textBoxSurname1.Clear(); textBoxSurname2.Clear();
            textBoxTelephone.Clear(); textBoxCellPhone.Clear();
            textBoxAddress.Clear(); textBoxPassword.Clear();
            textBoxPasswordConfirm.Clear(); radioButtonAdministrative.Select();
        }
        #endregion

        #region Modify user
        private void buttonModifyPwd_Click(object sender, EventArgs e)
        {
            panelModifyPwd.Visible = true;
            buttonModify.Enabled = false;
            buttonModifyCancel.Enabled = false;
        }

        private void buttonOkModifyPwd_Click(object sender, EventArgs e)
        {
            string message = UserController.UpdateUserPassword(textBoxModifyCi.Text, textBoxNewPwd.Text, textBoxReNewPwd.Text, textBoxOldPwd.Text);
            if (message == "La modificacion de contraseña ha sido exitosa!")
            {
                MessageBox.Show(null, message, "Cambio exitoso", MessageBoxButtons.OK, MessageBoxIcon.Information);
                panelModifyPwd.Visible = false;
                textBoxOldPwd.Clear();
                textBoxNewPwd.Clear();
                textBoxReNewPwd.Clear();
                buttonModify.Enabled = true;
                buttonModifyCancel.Enabled = true;
            }
            else
                MessageBox.Show(null, message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        private void buttonModify_Click(object sender, EventArgs e)
        {
            string ocupation = OcupationModifySelected();
            string message = UserController.UpdateUserData(textBoxModifyCi.Text, textBoxModifyName.Text, textBoxModifySurname1.Text, textBoxModifySurname2.Text, textBoxModifyTelephone.Text, textBoxModifyCellPhone.Text, textBoxModifyAddress.Text, ocupation);
            if (message == "Usuario modificado!")
            {
                MessageBox.Show(this, message, "Modificacion exitosa", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);
                RefreshUserGrid();
                tabControlUserManager.SelectedTab = tabPageSearchUser;
            }
            else
                MessageBox.Show(null, message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        private string OcupationModifySelected()
        {
            if (radioButtonModifyAdministrator.Checked)
                return "Administracion";
            else
                if (radioButtonModifySell.Checked)
                    return "Cajero";
                else
                    if (radioButtonModifyBuy.Checked)
                        return "Almacen";
                    else
                        return "Ninguno";
        }

        private void buttonCancelModifyPwd_Click(object sender, EventArgs e)
        {
            textBoxOldPwd.Clear();
            textBoxNewPwd.Clear();
            textBoxReNewPwd.Clear();
            panelModifyPwd.Visible = false;
            buttonModify.Enabled = true;
            buttonModifyCancel.Enabled = true;

        }

        private void buttonModifyCancel_Click(object sender, EventArgs e)
        {
            tabControlUserManager.SelectedTab = tabPageSearchUser;
        }

        private void OccupationModifySelected(string occupation)
        {
            if (occupation == "Administracion")
                radioButtonModifyAdministrator.Checked = true;
            else
                if (occupation == "Cajero")
                    radioButtonModifySell.Checked = true;
                else
                    if (occupation == "Almacen")
                        radioButtonModifyBuy.Checked = true;
        }
        #endregion

        #region Search User
        private void buttonViewUser_Click(object sender, EventArgs e)
        {
            if (userDataGridView.Rows.Count != 0)
            {
                textBoxViewUserCi.Text = userDataGridView.SelectedRows[0].Cells[0].Value.ToString();
                textBoxViewUserName.Text = userDataGridView.SelectedRows[0].Cells[3].Value.ToString();
                textBoxViewUserPatName.Text = userDataGridView.SelectedRows[0].Cells[4].Value.ToString();
                textBoxViewUserMatName.Text = userDataGridView.SelectedRows[0].Cells[5].Value.ToString();
                textBoxViewUserPhone.Text = userDataGridView.SelectedRows[0].Cells[1].Value.ToString();
                textBoxViewUserCell.Text = userDataGridView.SelectedRows[0].Cells[7].Value.ToString();
                textBoxViewUserAddress.Text = userDataGridView.SelectedRows[0].Cells[6].Value.ToString();
                textBoxViewOccupation.Text=userDataGridView.SelectedRows[0].Cells[8].Value.ToString();
                tabControlUserManager.SelectedTab = tabPageModifyUser;
            }
            else
                MessageBox.Show(null, "La lista esta vacia!", "No existen datos", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            tabControlUserManager.SelectedTab = tabPageLookUser;
        }

        private void buttonDeleteUser_Click(object sender, EventArgs e)
        {
            DataGridViewRow row = userDataGridView.CurrentRow;
            if (userDataGridView != null)
            {
                if (row == null)
                {
                    MessageBox.Show(this, "No se selecciono ningun registro ", "Seleccionar Campo", MessageBoxButtons.OK,
                    MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
                    MessageBoxOptions.RightAlign);
                }
                else
                {
                    value = (string)row.Cells[0].Value;
                    MessageBox.Show(this, "El usuario fue Eliminado", "Eliminar Usuario", MessageBoxButtons.OK,
                    MessageBoxIcon.Question, MessageBoxDefaultButton.Button1,
                    MessageBoxOptions.RightAlign);
                    bool controler = UserController.DeleteUser(value);
                }

            }
            else
            {
                MessageBox.Show(this, "No se selecciono ningun registro ", "Seleccionar Campo", MessageBoxButtons.OK,
                MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
                MessageBoxOptions.RightAlign);
            }
        }

        private void buttonQuit_Click(object sender, EventArgs e)
        {
            CleanAllSearchUserFills();
            tabControlUserManager.SelectedTab = tabPageMainUser;
        }

        private void CleanAllSearchUserFills()
        {
            textBoxSearchSurname1.Clear();
            textBoxSearchSurname2.Clear();
            textBoxSearchName.Clear();
            textBoxSearchCi.Clear();
        }

        private void buttonModifyUser_Click(object sender, EventArgs e)
        {
            if (userDataGridView.Rows.Count != 0)
            {
                textBoxModifyCi.Text = userDataGridView.SelectedRows[0].Cells[0].Value.ToString();
                textBoxModifyName.Text = userDataGridView.SelectedRows[0].Cells[3].Value.ToString();
                textBoxModifySurname1.Text = userDataGridView.SelectedRows[0].Cells[4].Value.ToString();
                textBoxModifySurname2.Text = userDataGridView.SelectedRows[0].Cells[5].Value.ToString();
                textBoxModifyTelephone.Text = userDataGridView.SelectedRows[0].Cells[1].Value.ToString();
                textBoxModifyCellPhone.Text = userDataGridView.SelectedRows[0].Cells[7].Value.ToString();
                textBoxModifyAddress.Text = userDataGridView.SelectedRows[0].Cells[6].Value.ToString();
                OccupationModifySelected(userDataGridView.SelectedRows[0].Cells[8].Value.ToString());
                tabControlUserManager.SelectedTab = tabPageModifyUser;
            }
            else
                MessageBox.Show(null, "La lista esta vacia!", "No existen datos", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        private void buttonSearch_Click(object sender, EventArgs e)
        {
            RefreshUserGrid();
        }

        private void RefreshUserGrid()
        {
            userDataGridView.DataSource = UserController.SearchAUser("%" + textBoxSearchCi.Text + "%", "%" + textBoxSearchName.Text + "%", "%" + textBoxSearchSurname1.Text + "%", "%" + textBoxSearchSurname2.Text + "%");
            userDataGridView.Columns["Password"].Visible = false;
            userDataGridView.Columns["Telephone"].Visible = false;
            userDataGridView.Columns["Address"].Visible = false;
            userDataGridView.Columns["Ocupation"].Visible = false;
            userDataGridView.Columns["Cellular"].Visible = false;
        }
        #endregion

        #region Text fields validators
        private void Validator_Only_Numbers_KeyPress(object sender, KeyPressEventArgs e)
        {
            Validator.ValidatorOnlyNumbers(sender, e);
        }

        private void Validator_only_Characters_KeyPress(object sender, KeyPressEventArgs e)
        {
            Validator.ValidatorOnlyCharacters(sender, e);
        }
        #endregion

        private void button1_Click(object sender, EventArgs e)
        {
            tabControlUserManager.SelectedTab = tabPageSearchUser;
        }
    }
}
